﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PM.Services;
using PM.Common;
using PM.Entity;

namespace PM.Site
{
    public partial class SecurityForm : System.Web.UI.Page
    {
        private bool editMode = false;

        public bool EditMode
        {
            get { return editMode; }
            set { editMode = value; }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null || Session["UserTypeId"] == null)
            {
                Response.Redirect("Login.aspx?RequestUrl=" + Request.Url.OriginalString);
            }
            else if (Session["UserTypeId"] != null)
            {
                if (Int32.Parse(Session["UserTypeId"].ToString()) == 0)
                {
                    this.Loading();
                    if (Session["SecurityId"] != null && (string)Session["SecurityId"] != "")
                    {
                        this.Loading(Session["SecurityId"].ToString());                        
                    }
                }
                else
                {
                    Response.Redirect("Default.aspx?RequestUrl=" + Request.Url.OriginalString);
                }
            }
            else
            {
                Response.Redirect("Login.aspx?RequestUrl=" + Request.Url.OriginalString);
            }
        }


        private void Loading()
        {
            if (!Page.IsPostBack)
            {
                EditMode = false;
                
            }
        }

        private void Loading(string securityId)
        {
            if (!Page.IsPostBack)
            {
                EditMode = true;
                Security security = SecurityServices.GetSecurityById(securityId);
                txbSecurityId.Text = security.SecurityId;
                txbName.Text = security.Name;
                txbMarginPercentage.Text = Convert.ToString(security.MarginPercentage);
                txbMktValue.Text = Convert.ToString(security.MarketValue);
                txbLastModDate.Text = security.LastModDate.ToShortDateString();

            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Security security = new Security();
            security.SecurityId = txbSecurityId.Text;
            security.Name = txbName.Text;
            double mktVal = 0;
            if (Double.TryParse(txbMktValue.Text, out mktVal))
                security.MarketValue = mktVal;
            double percentage = 0;
            if (Double.TryParse(txbMarginPercentage.Text, out percentage))
                security.MarginPercentage = percentage;
            security.LastModDate = DateTime.Now.Date;

            if (Session["SecurityId"] != null && (string)Session["SecurityId"] != "")
            {
                SecurityServices.Update(security);
                Session.Remove("SecurityId");
            }
            else
            {
                SecurityServices.Add(security);
            }

        }
    }
}
